// 设置禁止提交 git update-index --assume-unchanged cf.js
/**
 * 运行 node f.js args[0] args[1] args[2]
 * 
 * args[0] 参数1  代表项目名称的父文件夹路径 列子 /Users/zhaoxu/webFile/kjzz-tys-front/src/widgets
 * args[1]  参数2  代表想要处理文件夹名称
 * args[2]  参数3  '-c' 代表创建vue模版  '-m' 代表想文件夹下的images移入图片
 * 
 */

var args = process.argv.splice(2)

let fs = require("fs");
let path = require("path");
let dirname = args[0]
let folderName = args[1];
let index = args[2]

if(index === '-c'){
  createFile(dirname, folderName);
}else if(index === '-m'){
  removerImg(dirname, folderName)
}

function removerImg(dirname,folderName){
  console.log('------------------------------------')
  console.log('------------------------------------')
  console.log('------------------------------------')
  console.log('默认地址：', '/Users/zhaoxu/webFile/kjzz-tys-front/src/widgets')
  console.log('当前地址：', args[0])
  let projcetName = folderName.charAt(0).toUpperCase() + folderName.slice(1)
  let imgs = "images";
  if (args[3]) {
    imgs = args[3]
  }
  
  let fromFiles = "/Users/zhaoxu/webFile/images"
  try {
    const files = fs.readdirSync(fromFiles);
    files.forEach((item) => {
      let sourceFile = path.join(fromFiles, item);
      let destPath = "";
  
      destPath = path.join(
        dirname,
        projcetName,
        imgs,
        item
      )
  
      fs.rename(sourceFile, destPath, function (err) {
        if (err) throw err;
      });
    });
  
  
    console.log('移入图片：', files);
    console.log('------------------------------------')
    console.log('------------------------------------')
    console.log('------------------------------------')
  
  } catch (error) {
    console.log('移入图片失败，无效的文件路径');
  }
}


//  生成模板
function createFile(dirname, folderName) {
  console.log('------------------------------------')
  console.log('------------------------------------')
  console.log('------------------------------------')
  console.log('默认地址：', '/Users/zhaoxu/webFile/kjzz-tys-front/src/widgets')
  console.log('当前地址：', args[0])
  let createPath = dirname + "/" + convertText(folderName, 'fileText');
  fs.mkdir(createPath, () => {
    fs.mkdir(createPath + "/components", () => {
      fs.mkdir(createPath + "/components" + "/Viewapplication", () => {
        makeFiles(
          createPath + "/components" + "/Viewapplication" + "/index.vue",
          createTemplote('ViewApplication')
        );
      });
    });
    fs.mkdir(createPath + "/utils", () => {
      makeFiles(createPath + "/utils" + "/index.js", '');
    });
    fs.mkdir(createPath + "/scss", () => {
      makeFiles(
        createPath + "/scss" + "/index.scss",
        `//  Home page index style file
          .${convertText(folderName, 'classText')}{
            
          }
         `
      )
      makeFiles(
        createPath + "/scss" + "/style_deep.scss",
        "//  Group price penetration style"
      );
      makeFiles(
        createPath + "/scss" + "/jointly.scss",
        "//  Common styles of components and master files"
      );
      makeFiles(
        createPath + "/scss" + "/animation.scss",
        "// animation style file"
      );
      makeFiles(createPath + "/scss" + "/media.scss", "//  Media style file");
    });
    fs.mkdir(createPath + "/images", () => { });

    makeFiles(createPath + "/index.vue", createTemplote(folderName));
    console.log("< " + convertText(folderName, 'fileText') + " >模板生成成功！！");
  });


  console.log('------------------------------------')
  console.log('------------------------------------')
  console.log('------------------------------------')
}

//  生成文件
function makeFiles(filename, content) {
  fs.writeFile(filename, content, function (err) {
    if (err) {
      console.log("error");
      return;
    }
  });
}

//  读取文件
function readFileList(dir, filesList = []) {
  const files = fs.readdirSync(dir);
  files.forEach((item, index) => {
    var fullPath = path.join(dir, item);
    const stat = fs.statSync(fullPath);
    if (stat.isDirectory()) {
      //如果当前是文件目录
      readFileList(path.join(dir, item), filesList); //递归读取文件
    } else {
      filesList.push(fullPath);
    }
  });
  return filesList;
}

//  类名大小写转换
function createTemplote(className) {
  let text = convertText(className, 'classText')
  let Template = `
   <template>
       <div class="${text}">\n
       </div>
   </template>\n
   <script>
   export default {
       components: {\n
       },
       data() {
           return {\n
           };
       },
       props: {\n
       },
       created() {\n
       },
       methods: {\n
       },
       computed: {\n
       },
       watch: {\n
       },
   };
   </script>\n
   <style scoped lang="scss">\n
   @import "./scss/index";\n
   </style>`
  return Template
}

function convertText(className, type) {
  let ruleName = className.charAt(0).toUpperCase() + className.slice(1)
  let ruleName2 = ruleName.charAt(0).toLowerCase() + ruleName.slice(1)
  let ruleName3 = ruleName2.replace(/([A-Z])/g, '-$1').toLowerCase()
  if (type === 'fileText') {
    return className.charAt(0).toUpperCase() + className.slice(1)
  } else if (type === 'classText') {
    return ruleName3
  }
}


